package com.example.demo;

import com.example.demo.entity.Device;
import com.example.demo.service.DeviceService;

import com.example.demo.util.HttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;



@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);

    }
    @Bean
    public DataLoader dataLoader() {
        return new DataLoader();
    }

    static class DataLoader implements CommandLineRunner {
        @Autowired
        DeviceService deviceService;

        @Override
        public void run(String... strings) throws Exception {
            HttpClient httpClient = new HttpClient();
            long time = 0;
            long temp = 0;
            Device device = null;
            while(true){
                device= httpClient.getDevice("860315001001792").getData();
                if(device!=null&&device.getTimestamp()!=null){
                    temp = device.getLastRes().getTime();
                    if(temp>time){
                        deviceService.saveDevice(device);
                        time = temp;
                    }
                }
            }

        }
    }
}
